package com.itheima.myset;

import java.util.TreeSet;

public class StudentTreeSet implements Comparable<StudentTreeSet>{
    private String name;
    private int age;

    public StudentTreeSet() {
    }

    public StudentTreeSet(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentTreeSet{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }



    @Override
    public int compareTo(StudentTreeSet o) {
        //主要判断条件
        int result = this.age - o.age;
        //次要判断条件
        //看一下年龄比较的结果,如果是0,说明年龄相同,通过comparto方法比较字典顺序
        return  result == 0 ? this.name.compareTo(o.name) : result;

    }
}

class Test{
    public static void main(String[] args) {
        TreeSet<StudentTreeSet> ts = new TreeSet<>();
        StudentTreeSet s1 = new StudentTreeSet("cx阿巴阿巴",18);
        StudentTreeSet s2 = new StudentTreeSet("zz咪咕哔咕",23);
        StudentTreeSet s3 = new StudentTreeSet("aaac阿卡丽",16);
        StudentTreeSet s4 = new StudentTreeSet("aaAz阿狸",16);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);

        System.out.println(ts);
    }
}